<?php

class ngWidgetFormRichDate extends sfWidgetFormDate {
	public function render($name, $value = null, $attributes = array(), $errors = array()) {
		
		sfContext::getInstance ()->getConfiguration ()->loadHelpers ( array ('Date', 'Form' ) );
		sfContext::getInstance ()->getResponse ()->addJavaScript ( sfConfig::get ( 'sf_prototype_web_dir' ) . '/js/prototype', 'first' );
		
		/* if ($value === null)
			$value = date ( 'Y.m.d.', time () );
		else
    */  // ez felesleges. ha valahova mégis kellett, akkor oldjuk meg projekt szinten és ne a keretrendszeres widgetbe. köszöntem
		
		//if ($value !== 0) {
		if ($value) {
		  $value = preg_replace ( "/\-/", ".", $value );
			if (! preg_match ( "/\.$/", $value ))
				$value .= ".";
		  //$value = date("Y.m.d.", strtotime($value));
				
		}
		
		$attributes = array_merge ( $this->attributes, array ('rich' => 'true', 'size' => '10' ), $attributes );
		$retVal = "<div class='date' >" . input_date_tag ( $name, null, $attributes ) . "</div>";
		
		if (isset ( $attributes ['readonly'] )) {
			//ha readonly elrejtjük a gombot
			$id_inputField = isset ( $attributes ['id'] ) ? $attributes ['id'] : get_id_from_name ( $name );
			$id_calendarButton = 'trigger_' . $id_inputField;
			
			$readOnly = $attributes ['readonly'] ? "\$('" . $id_calendarButton . "').hide()" : '';
		} else {
			$readOnly = '';
		}
		if ($value !== 0)
			$retVal .= "<script type=\"text/javascript\" >\$('" . get_id_from_name ( $name ) . "').value='" . $value . "';  " . $readOnly . "</script>";
		return $retVal;
	}

}